import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 招生计划专业组匹配测试类
 * 用于验证第7940行招生计划查询中的专业组智能匹配逻辑
 */
public class 招生计划专业组匹配测试 {
    
    public static void main(String[] args) {
        招生计划专业组匹配测试 test = new 招生计划专业组匹配测试();
        test.testEnrollmentPlanSubjectGroupMatching();
    }
    
    public void testEnrollmentPlanSubjectGroupMatching() {
        System.out.println("=== 招生计划专业组匹配测试 ===");
        
        // 模拟专业录取信息数据（MajorAdmissionInfo）
        List<MockMajorAdmissionInfo> majors = Arrays.asList(
            new MockMajorAdmissionInfo("清华大学", "计算机科学与技术", "1093（W003）"),
            new MockMajorAdmissionInfo("北京大学", "软件工程", "1094（W005）"),
            new MockMajorAdmissionInfo("复旦大学", "电子信息工程", "1095（L001）"),
            new MockMajorAdmissionInfo("上海交通大学", "机械工程", "1096（M002）"),
            new MockMajorAdmissionInfo("中山大学", "临床医学", "1097（Y003）")
        );
        
        // 模拟招生计划数据（CollegeEnrollmentPlan）
        List<MockEnrollmentPlan> enrollmentPlans = Arrays.asList(
            // 完全匹配的情况
            new MockEnrollmentPlan("清华大学", "计算机科学与技术", "（W003）", 30),
            new MockEnrollmentPlan("北京大学", "软件工程", "（W005）", 25),
            
            // 数字部分匹配的情况（W003 vs 003）
            new MockEnrollmentPlan("清华大学", "计算机科学与技术（实验班）", "（003）", 15),
            new MockEnrollmentPlan("复旦大学", "电子信息工程", "（001）", 20),  // L001 vs 001
            
            // 字母+数字 vs 纯数字的情况
            new MockEnrollmentPlan("上海交通大学", "机械工程", "（002）", 35),  // M002 vs 002
            new MockEnrollmentPlan("中山大学", "临床医学", "（003）", 40),  // Y003 vs 003
            
            // 不匹配的情况
            new MockEnrollmentPlan("清华大学", "计算机科学与技术", "（W999）", 10),
            new MockEnrollmentPlan("北京大学", "软件工程", "（L888）", 12)
        );
        
        System.out.println("\n=== 专业组匹配测试 ===");
        
        for (MockMajorAdmissionInfo major : majors) {
            System.out.println("\n专业: " + major.schoolName + " - " + major.majorName + " (专业组: " + major.subjectGroup + ")");
            
            // 提取专业组信息
            String extractedSubjectGroup = extractSubjectGroupFromMajorName(major.subjectGroup);
            System.out.println("  提取的专业组: " + extractedSubjectGroup);
            
            // 查找匹配的招生计划
            List<MockEnrollmentPlan> matchedPlans = new ArrayList<>();
            for (MockEnrollmentPlan plan : enrollmentPlans) {
                if (plan.schoolName.equals(major.schoolName) && 
                    isMajorNameSimilar(plan.majorName, major.majorName) &&
                    isSpecialGroupMatched(extractedSubjectGroup, plan.specialGroupName)) {
                    matchedPlans.add(plan);
                }
            }
            
            System.out.println("  匹配的招生计划数: " + matchedPlans.size());
            for (MockEnrollmentPlan plan : matchedPlans) {
                System.out.println("    - " + plan.majorName + " (专业组: " + plan.specialGroupName + ", 招生人数: " + plan.enrollmentNumber + ")");
            }
            
            if (matchedPlans.isEmpty()) {
                System.out.println("    ❌ 未找到匹配的招生计划");
            } else {
                System.out.println("    ✅ 找到 " + matchedPlans.size() + " 个匹配的招生计划");
            }
        }
        
        // 验证专业组匹配逻辑
        System.out.println("\n=== 专业组匹配逻辑验证 ===");
        
        String[][] testCases = {
            {"W003", "（W003）", "true"},   // 完全匹配
            {"W003", "（003）", "true"},    // 数字部分匹配
            {"L001", "（001）", "true"},    // 字母+数字 vs 数字
            {"M002", "（002）", "true"},    // 字母+数字 vs 数字
            {"Y003", "（003）", "true"},    // 字母+数字 vs 数字
            {"W003", "（W999）", "false"},  // 不匹配
            {"L001", "（L888）", "false"},  // 不匹配
            {"003", "（W003）", "true"},    // 反向匹配：数字 vs 字母+数字
            {"001", "（L001）", "true"},    // 反向匹配：数字 vs 字母+数字
        };
        
        int passedTests = 0;
        for (String[] testCase : testCases) {
            String subjectGroup = testCase[0];
            String specialGroupName = testCase[1];
            boolean expected = Boolean.parseBoolean(testCase[2]);
            
            boolean actual = isSpecialGroupMatched(subjectGroup, specialGroupName);
            boolean passed = actual == expected;
            
            System.out.println(String.format("%s vs %s: 期望=%s, 实际=%s %s", 
                subjectGroup, specialGroupName, expected, actual, passed ? "✅" : "❌"));
            
            if (passed) {
                passedTests++;
            }
        }
        
        System.out.println("\n=== 测试结果 ===");
        System.out.println(String.format("专业组匹配测试: %d/%d 通过 (%.1f%%)", 
            passedTests, testCases.length, (passedTests * 100.0 / testCases.length)));
        
        if (passedTests == testCases.length) {
            System.out.println("🎉 所有测试通过！招生计划专业组智能匹配功能正常工作。");
        } else {
            System.out.println("⚠️ 部分测试失败，需要检查专业组匹配逻辑。");
        }
    }
    
    /**
     * 从专业组字符串中提取专业组信息
     */
    private String extractSubjectGroupFromMajorName(String subjectGroup) {
        if (subjectGroup == null || subjectGroup.trim().isEmpty()) {
            return null;
        }
        
        String trimmed = subjectGroup.trim();
        
        // 1. 提取中文括号内的内容：（003）或（W003）
        Pattern chineseParenPattern = Pattern.compile("（([^）]+)）");
        Matcher chineseMatcher = chineseParenPattern.matcher(trimmed);
        if (chineseMatcher.find()) {
            return chineseMatcher.group(1);
        }
        
        // 2. 提取英文括号内的内容：(003)或(W003)
        Pattern englishParenPattern = Pattern.compile("\\(([^)]+)\\)");
        Matcher englishMatcher = englishParenPattern.matcher(trimmed);
        if (englishMatcher.find()) {
            return englishMatcher.group(1);
        }
        
        // 3. 提取数字+括号格式：1093（W003）中的W003
        Pattern numberParenPattern = Pattern.compile("\\d+[（(]([^）)]+)[）)]");
        Matcher numberMatcher = numberParenPattern.matcher(trimmed);
        if (numberMatcher.find()) {
            return numberMatcher.group(1);
        }
        
        return null;
    }
    
    /**
     * 专业组智能匹配逻辑
     */
    private boolean isSpecialGroupMatched(String subjectGroup, String specialGroupName) {
        if (subjectGroup == null || specialGroupName == null) {
            return false;
        }
        
        // 1. 直接匹配
        if (subjectGroup.equals(specialGroupName)) {
            return true;
        }
        
        // 2. 提取专业组编号进行匹配
        String extractedGroupCode = extractSpecialGroupCode(specialGroupName);
        String existingGroupCode = extractSpecialGroupCode(subjectGroup);
        
        if (extractedGroupCode != null && existingGroupCode != null) {
            return isGroupCodeMatched(extractedGroupCode, existingGroupCode);
        }
        
        return false;
    }
    
    /**
     * 从专业组字符串中提取专业组编号
     */
    private String extractSpecialGroupCode(String specialGroup) {
        if (specialGroup == null || specialGroup.trim().isEmpty()) {
            return null;
        }
        
        String trimmed = specialGroup.trim();
        
        // 1. 提取中文括号内的内容：（003）或（W003）
        Pattern chineseParenPattern = Pattern.compile("（([^）]+)）");
        Matcher chineseMatcher = chineseParenPattern.matcher(trimmed);
        if (chineseMatcher.find()) {
            return chineseMatcher.group(1);
        }
        
        // 2. 提取英文括号内的内容：(003)或(W003)
        Pattern englishParenPattern = Pattern.compile("\\(([^)]+)\\)");
        Matcher englishMatcher = englishParenPattern.matcher(trimmed);
        if (englishMatcher.find()) {
            return englishMatcher.group(1);
        }
        
        // 3. 如果是字母+数字格式（如W003），直接返回
        if (trimmed.matches("[A-Za-z]\\d+")) {
            return trimmed;
        }
        
        // 4. 如果是纯数字格式（如003），直接返回
        if (trimmed.matches("\\d+")) {
            return trimmed;
        }
        
        return null;
    }
    
    /**
     * 专业组编号智能匹配
     */
    private boolean isGroupCodeMatched(String code1, String code2) {
        if (code1 == null || code2 == null) {
            return false;
        }
        
        // 1. 完全相同
        if (code1.equals(code2)) {
            return true;
        }
        
        // 2. 数字部分相同（如W003和003，L001和001）
        String number1 = extractNumberPart(code1);
        String number2 = extractNumberPart(code2);
        
        if (number1 != null && number2 != null && number1.equals(number2)) {
            return true;
        }
        
        return false;
    }
    
    /**
     * 从专业组编号中提取数字部分
     */
    private String extractNumberPart(String code) {
        if (code == null || code.trim().isEmpty()) {
            return null;
        }
        
        String trimmed = code.trim();
        
        // 如果是纯数字，直接返回
        if (trimmed.matches("\\d+")) {
            return trimmed;
        }
        
        // 如果是字母+数字格式，提取数字部分
        Pattern pattern = Pattern.compile("[A-Za-z]*(\\d+)");
        Matcher matcher = pattern.matcher(trimmed);
        if (matcher.find()) {
            return matcher.group(1);
        }
        
        return null;
    }
    
    /**
     * 专业名称相似性判断（简化版）
     */
    private boolean isMajorNameSimilar(String name1, String name2) {
        if (name1 == null || name2 == null) {
            return false;
        }
        
        // 去除括号内容后比较
        String clean1 = name1.replaceAll("[（(][^）)]*[）)]", "").trim();
        String clean2 = name2.replaceAll("[（(][^）)]*[）)]", "").trim();
        
        return clean1.contains(clean2) || clean2.contains(clean1);
    }
    
    /**
     * 模拟专业录取信息类
     */
    static class MockMajorAdmissionInfo {
        String schoolName;
        String majorName;
        String subjectGroup;
        
        public MockMajorAdmissionInfo(String schoolName, String majorName, String subjectGroup) {
            this.schoolName = schoolName;
            this.majorName = majorName;
            this.subjectGroup = subjectGroup;
        }
    }
    
    /**
     * 模拟招生计划类
     */
    static class MockEnrollmentPlan {
        String schoolName;
        String majorName;
        String specialGroupName;
        int enrollmentNumber;
        
        public MockEnrollmentPlan(String schoolName, String majorName, String specialGroupName, int enrollmentNumber) {
            this.schoolName = schoolName;
            this.majorName = majorName;
            this.specialGroupName = specialGroupName;
            this.enrollmentNumber = enrollmentNumber;
        }
    }
}